package com.wrupple.muba.catalogs.client.widgets.fields.cells;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.wrupple.muba.catalogs.client.activity.process.CatalogEditingProcess;
import com.wrupple.muba.catalogs.client.activity.process.task.CatalogInteractionState.Mode;
import com.wrupple.muba.catalogs.client.cms.service.CatalogManagementSystem;
import com.wrupple.muba.catalogs.client.cms.service.CatalogManager;
import com.wrupple.muba.catalogs.client.widgets.fields.cells.templates.CatalogKeyTemplates;
import com.wrupple.muba.catalogs.domain.CatalogEntry;
import com.wrupple.muba.catalogs.domain.JsCatalogKey;
import com.wrupple.muba.common.client.JSOHelper;
import com.wrupple.muba.common.shared.Process;
import com.wrupple.muba.common.shared.State.ProcessManager;
import com.wrupple.vegetate.domain.FieldDescriptor;

public class GenericValueCell extends  AbstractDelegatingEditableField<JsCatalogKey>  {

	private CatalogManagementSystem cms;
	private CatalogKeyTemplates template;
	private NumberFormat currencyFormat;
	
	public GenericValueCell( CatalogManagementSystem cms,EventBus bus, ProcessManager pm,
			FieldDescriptor d, Mode mode) {
		super(bus, pm, d, mode);
		this.cms=cms;
		template = GWT.create(CatalogKeyTemplates.class);
		currencyFormat = NumberFormat.getCurrencyFormat();
	}

	@Override
	protected String getProcessLocalizedName() {
		return super.fieldDescriptor.getForeignCatalog();
	}

	@Override
	protected Process<JsCatalogKey, JsCatalogKey> getDelegateProcess() {
		String catalog = super.fieldDescriptor.getForeignCatalog();
		CatalogManager<JsCatalogKey> manager = cms.getContentManager(catalog);
		CatalogEditingProcess<JsCatalogKey> editingProcess;
		switch (mode) {
			case CREATE:
				// modify foreign entry ¿really?
				editingProcess = manager.getEditingProcess(Mode.UPDATE, getBus());
				break;
			default:
				editingProcess = manager.getEditingProcess(mode, getBus());
				break;
		}
		Process regreso = editingProcess;
		return regreso;
	}

	@Override
	protected void renderAsInput(
			com.google.gwt.cell.client.Cell.Context context,
			JsCatalogKey value,
			SafeHtmlBuilder sb,
			com.wrupple.muba.catalogs.client.widgets.fields.cells.AbstractEditableField.FieldData<JsCatalogKey> viewData) {
		if(value==null){
			
		}else{
			String key=value.getId();
			String name=value.getName();
			if(key==null){
				key = "";
			}
			if(name == null){
				name = value.getValue();
			}
			if(name == null){
				name = "";
			}
			SafeHtml output = template.value(key, name);
			sb.append(output);
		}
	}

	@Override
	protected void renderReadOnly(
			com.google.gwt.cell.client.Cell.Context context,
			JsCatalogKey value,
			SafeHtmlBuilder sb,
			com.wrupple.muba.catalogs.client.widgets.fields.cells.AbstractEditableField.FieldData<JsCatalogKey> viewData) {
		if(value==null){
			
		}else{
			String key=value.getId();
			String name=value.getName();
			if(key==null){
				key = "";
			}
			if(name == null){
				name = value.getValue();
			}
			if(name == null){
				Double price = JSOHelper.getAttributeAsDouble(value, "price");
				if(price!=null){
					name = currencyFormat.format(price);
				}
			}
			if(name == null){
				name = "";
			}
			SafeHtml output = template.value(key, name);
			sb.append(output);
		}
		
	}

	@Override
	protected JsCatalogKey getCurrentInputValue(Element parent, boolean isEditing) {
		JsCatalogKey regreso = JavaScriptObject.createObject().cast();
		return regreso;
	}

}
